import { observable, action } from 'mobx'
import notificationWithIcon from "../../common/notificationWithIcon";
import axios from "../../../common/request"
import {message} from "antd/lib/index";

class RegisterStore {
    @observable isSend=0
    actions = {
        sendCode:action((phone)=>{
            if(this.isSend===0){
                this.isSend=60
                axios.post("/api/common/register/code",{phoneNo:phone})
                    .then((response)=> {
                        if(response.data.status.code===1){
                            let time=setInterval(()=>{
                                if(this.isSend>0){
                                    this.isSend--;
                                }
                                else{
                                    clearInterval(time)
                                }
                            },1000)
                        }
                    })
            }
            else{
                notificationWithIcon("error","发送过于频繁","")
            }
        }),
        isRegister:action((value,form)=>{
            axios.get("/api/common/register/status?phoneNo="+value)
                .then((response)=>{
                    if(response.data.status.code===1012){
                        form.setFields({
                            phoneNo: {
                                value: "",
                                errors: [new Error(response.data.status.message)],
                            },
                        });
                        message.error(response.data.status.message, 5)
                    }
                })
        }),
        register:action((values,toggle,form)=>{
            axios.post("/api/common/register",values)
                .then((response)=>{
                    toggle(false)
                    if(response.data.status.code===1){
                        if(window.location.search!=""){
                            window.location.href=window.location.search.substring(6,window.location.search.length)
                        }
                       else{
                            window.location.href="/"
                        }
                    }
                    else if(response.data.status.code===1012){
                        form.setFields({
                            phoneNo: {
                                value: "",
                                errors: [new Error(response.data.status.message)],
                            },
                        });
                        message.error(response.data.status.message, 5)
                    }
                    else if(response.data.status.code===1014){
                        form.setFields({
                            code: {
                                value: "",
                                errors: [new Error(response.data.status.message)],
                            },
                        });
                        message.error(response.data.status.message, 5)
                    }
                })
                .catch(function (error) {
                    toggle(false)
                    notificationWithIcon('error','注册失败',error.response)
                    console.log(error);
                });
        }),
    }
}
export default new RegisterStore()